package com.hsy.crm.web.service.impl;

import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.entity.*;
import com.hsy.crm.web.domain.ex.ServiceException;
import com.hsy.crm.web.service.asyn.AsynService;
import com.hsy.crm.web.utils.DateUtils;
import com.hsy.crm.web.utils.Longs;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MessageService {

    public void addMessage(Order dbOrder, List<OrderDetail> orderDetails, int actionType){
        setMessage(dbOrder.getId(),dbOrder.getOrderType(),actionType,dbOrder.getCompanyId(),dbOrder.getCustomerId(),orderDetails);
    }

    public void addMessage(SplitProduct dbOrder,List<SplitProductDetail> orderDetails, int actionType){
        setMessage(dbOrder.getId(),App.BILL_TYPE.ORDER_CZ,actionType,dbOrder.getCompanyId(),dbOrder.getCustomerId(),orderDetails);
    }

    public void addMessage(TransferOrder dbOrder,List<TransferOrderDetail> orderDetails,int actionType){
        setMessage(dbOrder.getId(),App.BILL_TYPE.ORDER_DB,actionType,dbOrder.getCompanyId(),dbOrder.getCustomerId(),orderDetails);
    }

    public void addMessage(StoneOther dbOrder,List<StoneOtherDetail> orderDetails,int actionType){
        setMessage(dbOrder.getId(),dbOrder.getOrderType(),actionType,dbOrder.getCompanyId(),dbOrder.getCustomerId(),orderDetails);
    }


    /**
     * 主库存可以在这里添加 ？？
     * @param targetId
     * @param targetType
     * @param actionType
     * @param companyId
     * @param customerId
     * @return
     */
    private void setMessage(Long targetId,Integer targetType,Integer actionType,Long companyId,String customerId,List<?> orderDetails){
        Message message = new Message();
        message.setCustomerId(customerId);
        message.setActionType(actionType);
        message.setCompanyId(companyId);
        message.setTargetId(targetId);
        message.setTargetType(targetType);
        message.setTime(DateUtils.millis());
        checkMessage(message);
        message.setOrderDetails(orderDetails);

        AsynService.addMessage(message);
    }

    private static void checkMessage(Message message){
        if(Longs.isEmpty(message.getCompanyId())){
            throw new ServiceException("add message : company id is error");
        }
    }

}
